import 'dart:convert';

import 'package:largefriends/entity/json/json_parse_utils.dart';

import '../../config/lf_im_type.dart';
import 'chat_new_message_entity.dart';

class LfMessageEntity {
  LfMessageEntity({
    this.type,
    this.typeId,
    this.duration,
    this.url,
    this.width,
    this.height,
    this.fromUid,
    this.fromName,
    this.toUid,
    this.content,
    this.time,
    this.messageId,
    this.roomId,
    this.showRecall,
    this.localId,
    this.xInfo,
    this.sendState,
    this.errorCode,
    this.lastMessageCreateTime = 0,
    this.groupId,
    this.groupType,
    this.clientId,
    this.avatar,
    this.replyInfo,
  });

  factory LfMessageEntity.fromJson(Map<String, dynamic> jsonRes) =>
      LfMessageEntity(
        type: jsonRes.asString('type'),
        typeId: jsonRes.asString('type_id'),
        duration: jsonRes.asInt('duration'),
        url: jsonRes.asString('url'),
        width: jsonRes.asDouble('width'),
        height: jsonRes.asDouble('height'),
        fromUid: jsonRes.asString('from_uid'),
        fromName: jsonRes.asString('from_name'),
        toUid: jsonRes.asString('to_uid'),
        content: jsonRes.asString('content'),
        time: jsonRes.asInt('time'),
        messageId: jsonRes.asString('message_id'),
        roomId: jsonRes.asString('room_id'),
        showRecall: jsonRes.asInt('show_recall'),
        localId: jsonRes.asString('local_id'),
        xInfo: jsonRes.asString('x_info'),
        sendState: jsonRes.asInt('send_state'),
        errorCode: jsonRes.asInt('error_code'),
        lastMessageCreateTime: jsonRes.asInt('last_check_time'),
        groupId: jsonRes.asString('group_id'),
        groupType: jsonRes.asString('group_type'),
        clientId: jsonRes.asString('client_id'),
        avatar: jsonRes.asString('avatar'),
        replyInfo: jsonRes.asBean('replyInfo', (v) => ReplyInfo.fromJson(v)),

        ///chatroom有时候会返回
      );

  String? type;
  String? typeId;
  int? duration;
  String? url;
  double? width;
  double? height;
  String? fromUid;
  String? fromName;
  String? toUid;
  String? content;
  int? time;
  String? messageId;
  String? roomId;
  int? showRecall;
  String? localId;
  String? xInfo;
  String? clientId;
  String? groupId;
  String? groupType;
  String? avatar;
  ReplyInfo? replyInfo;
  int? sendState = MessageStatusEnum.success.value;
  int lastMessageCreateTime = 0;

  ///实时消息，所以默认就是发送成功
  int? errorCode = 0;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'type': type,
        'type_id': typeId,
        'duration': duration,
        'url': url,
        'width': width,
        'height': height,
        'from_uid': fromUid,
        'from_name': fromName,
        'to_uid': toUid,
        'content': content,
        'time': time,
        'message_id': messageId,
        'room_id': roomId,
        'show_recall': showRecall,
        'local_id': localId,
        'x_info': xInfo,
        'send_state': sendState,
        'error_code': errorCode,
        'lastMessageCreateTime': lastMessageCreateTime,
        'client_id': clientId,
        'group_type': groupType,
        'group_id': groupId,
        'avatar': avatar,
        'replyInfo': replyInfo,
      };

  ChatNewMessageEntity toChatNewMessageEntity(Sender sender) =>
      ChatNewMessageEntity(
          messageType: typeId,
          duration: duration.toString(),
          url: url,
          width: width,
          height: height,
          message: content,
          historyId: messageId,
          created: time!,
          sendState: MessageStatusEnum.success.value,
          replyInfo: replyInfo,
          sender: sender);
}
